//
//  AWHomeToolBar.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/9.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit

class AWHomeToolBar: UIView {
    
    var statusesViewModel: AWStatusesViewModel? {
        
        didSet {
            
            retweetBtn?.setTitle(statusesViewModel?.reposts_count, for: .normal)
            
            commentBtn?.setTitle(statusesViewModel?.comments_count, for: .normal)
            
            unlikeBtn?.setTitle(statusesViewModel?.attitudes_count, for: .normal)
            
        }
        
    }
    
    var retweetBtn: UIButton?
    
    var commentBtn: UIButton?
    
    var unlikeBtn: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
        
    }
    
    private func setupUI() {
        
        let retweetButton = createButton(title: "  转发", imageName: "timeline_icon_retweet")
        
        retweetBtn = retweetButton
        
        let commentButton = createButton(title: "  评论", imageName: "timeline_icon_comment")
        
        commentBtn = commentButton
        
        let unlikeButton = createButton(title: "  赞", imageName: "timeline_icon_unlike")
        
        unlikeBtn = unlikeButton
        
        let firstLine = createLineView()
        
        let secondLine = createLineView()
        
        addSubview(retweetButton)
        
        addSubview(commentButton)
        
        addSubview(unlikeButton)
        
        addSubview(firstLine)
        
        addSubview(secondLine)
        
        retweetButton.snp.makeConstraints { (make) in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton)
            
        }
        
        commentButton.snp.makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton.snp.trailing)
            make.width.equalTo(unlikeButton)
            
        }
        
        unlikeButton.snp.makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton.snp.trailing)
            
        }
        
        firstLine.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(retweetButton.snp.trailing)
            make.centerY.equalTo(self)
            
        }
        secondLine.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(commentButton.snp.trailing)
            make.centerY.equalTo(self)
            
        }
        
    }
    
    private func createButton(title: String, imageName: String) -> UIButton {
        
        let button = UIButton(title: title, normalTextColor: UIColor.darkGray, highlightedTextColor: nil, fontSize: 14)
        
        button.adjustsImageWhenHighlighted = false
        
        button.setImage(UIImage(named: imageName), for: .normal)
        
        button.setBackgroundImage(#imageLiteral(resourceName: "timeline_card_bottom_background"), for: .normal)
        
        return button
    }
    
    private func createLineView() -> UIImageView {
        
        let imageView = UIImageView(image: #imageLiteral(resourceName: "timeline_card_bottom_line"))
        
        return imageView
        
    }

}
